package Dao;

import domain.Teacher;

public class TeacherDao {

    static Teacher[] teachers = new Teacher[5];

    static {
        Teacher t1 = new Teacher("001", "zs", "23", "1995");
        Teacher t2 = new Teacher("002", "ls", "24", "1996");
        Teacher t3 = new Teacher("003", "ww", "25", "1997");
        teachers[0] = t1;
        teachers[1] = t2;
        teachers[2] = t3;
    }

    public boolean add(Teacher t) {

        //判断老师数组是否为null，若为null则可以往里面放老师信息
        //假设数组里面装满
        int index = -1;
        //看数组中遍历出来的每一个元素是否为null，若为null则记录下来这个索引位置
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            return false;//装满了
        } else {
            teachers[index] = t;//没有装满，则把老师信息放到这个索引位置
        }
        return true;
    }

    public Teacher[] findAll() {

        return teachers;
    }

    public void delete(String delId) {

        int index = getIndex(delId);
        teachers[index] = null;
    }

    public int getIndex(String delId) {

        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher != null && teacher.getId().equals(delId)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public void update(String updeId, Teacher newT) {

        int index = getIndex(updeId);
        teachers[index] = newT;
    }
}
